/*
 * commons is a based project implemented
 * Copyright (C) 2024 Jasmine
 */
package com.commons.database.base;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.commons.core.domain.dto.BaseDTO;
import com.commons.core.domain.dto.IdBean;
import com.commons.core.validator.ValidGroup;
import com.github.pagehelper.PageInfo;

import io.swagger.v3.oas.annotations.Operation;

/**
 * 核心公共controller基类
 *
 * @author Jasmine
 * @version 1.0
 * @date 2023-11-15 13:54:54
 * @see BaseController
 * @param <T> 实体类
 * @param <D> 查询条件
 * @param <V> 详情信息
 * @param <S> 业务接口
 * @since JDK1.8
 */
public abstract class BaseController<T, D extends BaseDTO, V, S extends BaseService<T, D, V>> {

    @Autowired
    protected S baseService;

    @Operation(summary = "添加")
    @PostMapping("/add")
    public Integer add(@RequestBody D modelDTO) {
        return baseService.add(modelDTO);
    }

    @Operation(summary = "详情")
    @PostMapping("/detail")
    public V detail(@RequestBody @Validated({ValidGroup.ID.class}) IdBean<Long> idBean) {
        return baseService.get(idBean.getId());
    }

    @Operation(summary = "列表")
    @PostMapping("/list")
    public List<V> list(@RequestBody @Validated({ValidGroup.Query.class}) D modelDTO) {
        return baseService.list(modelDTO);
    }

    @Operation(summary = "分页列表")
    @PostMapping("/page")
    public PageInfo<V> page(@RequestBody @Validated({ValidGroup.Query.class}) D modelDTO) {
        return baseService.page(modelDTO);
    }

    @Operation(summary = "修改记录")
    @PostMapping("/update")
    public Boolean update(@RequestBody @Validated({ValidGroup.Update.class, ValidGroup.ID.class}) D modelDTO) {
        return baseService.update(modelDTO);
    }

    @Operation(summary = "删除")
    @PostMapping("/delete")
    public Boolean delete(@RequestBody @Validated({ValidGroup.ID.class}) IdBean<Long> idBean) {
        return baseService.delete(idBean.getId());
    }
}